package cn.community.system.service.impl;

import cn.community.system.domain.Device;
import cn.community.system.mapper.DeviceMapper;
import cn.community.system.query.QueryObject;
import cn.community.system.service.IDeviceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DeviceServiceImpl implements IDeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public void delete(Long id) {
        deviceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Device device) {
        if (device.getImgUrl()==""){
            device.setImgUrl(null);
        }
        deviceMapper.insert(device);
    }

    @Override
    public Device get(Long id) {
        return deviceMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Device> listAll() {
        return deviceMapper.selectAll();
    }

    @Override
    public void update(Device device) {
        if (device.getImgUrl()==""){
            device.setImgUrl(null);
        }
        deviceMapper.updateByPrimaryKey(device);
    }

    public PageInfo query(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询结果集
        List<Device> list = deviceMapper.selectForList(qo);
        return new PageInfo(list);
    }

}
